package com.taoyibao.hongcanting.ui.login.delegate;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.delegate.BackToolbarDelegate;
import com.taoyibao.hongcanting.ui.login.activity.RegisterActivity;
import com.taoyibao.hongcanting.ui.webview.ProtocolArea;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;

/**
 * @author TangAnna
 * @description:登录
 * @date :${DATA} 15:40
 */
public class LoginDelegate extends BackToolbarDelegate implements View.OnClickListener, TextWatcher {
    private EditText mEtPhone;
    private EditText mEtVCode;
    private ProtocolArea mProtocolArea;

    @Override
    public int getRootLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        setTitle("登录");
        mProtocolArea = get(R.id.protocolArea);
        mProtocolArea.setData(0);
        get(R.id.tv_login_register).setOnClickListener(this);
        mEtPhone = get(R.id.et_login_phone);
        mEtVCode = get(R.id.et_login_vcode);
        mEtPhone.addTextChangedListener(this);
        mEtVCode.addTextChangedListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_login_register://去注册
                getActivity().startActivity(new Intent(getActivity(), RegisterActivity.class));
                break;
        }
    }

    /**
     * 判断是否可登录
     *
     * @return
     */
    public boolean isCanLogin() {
        if (!isCanVCode()) {
            return false;
        } else if (TextUtils.isEmpty(getVCodeStr())) {
            ToastUtils.showToast(CodeUtils.getIdString(R.string.string_inputVCode));
            return false;
        }
        return true;
    }

    /**
     * 判断是否可以获取验证码
     *
     * @return
     */
    public boolean isCanVCode() {
        if (TextUtils.isEmpty(getPhone())) {
            //提示输入手机号
            ToastUtils.showToast(CodeUtils.getIdString(R.string.string_hintEmptyPhone));
            return false;
        } else if (!CodeUtils.judgePhoneNumber(getPhone())) {
            //手机号不合法
            ToastUtils.showToast(getActivity().getResources().getString(R.string.string_hintPhoneError));
            return false;
        }
        return true;
    }

    /**
     * 获取输入的手机号
     *
     * @return
     */
    public String getPhone() {
        return ((EditText) get(R.id.et_login_phone)).getText().toString();
    }

    /**
     * 获取输入的验证码
     *
     * @return
     */
    public String getVCodeStr() {
        return ((EditText) get(R.id.et_login_vcode)).getText().toString();
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (!TextUtils.isEmpty(mEtPhone.getText().toString().trim()) && !TextUtils.isEmpty(mEtVCode.getText().toString().trim())) {
            ((Button) get(R.id.btn_login)).setEnabled(true);
        } else {
            ((Button) get(R.id.btn_login)).setEnabled(false);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
